import 'dart:typed_data';

import 'package:single/main.dart';


//询查标签
inventory(){
  var array = [0xdd, 0x11, 0xef, 0x09, 0xff, 0x01, 0x00,0x00,0x00];
  // var (byte1,byte2) = analyzeCRC(array);
  // array.add(byte1);
  // array.add(byte2);
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

//开门
openDoor(){
  var array = [0xdd, 0x11, 0xef, 0x0c, 0x00, 0xc2, 0x00,0x01,0x00,0x00,0x11,0x22];
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}
//查询状态
checkStatus(){
  var array = [0xdd, 0x11, 0xef, 0x0c, 0x00, 0xc3, 0x00,0x00,0x00,0x00,0x11,0x22];
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

//开灯
openLight(){
  var array = [0xdd, 0x11, 0xef, 0x0c, 0x00, 0xc1, 0x00,0x01,0x00,0x00,0x11,0x22];
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}
//关灯
closeLight(){
  var array = [0xdd, 0x11, 0xef, 0x0c, 0x00, 0xc1, 0x00,0x00,0x00,0x00,0x11,0x22];
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}
//读块
readBlock(int ant,String uid){
  List<int> xx = [];
  if(uid.length%2 == 1){
    uid = "${uid}0";
  }
  int length = uid.length;
  int i = 0;
  while(i<length){
    var x = int.parse("0x${uid.substring(i,i+2)}");
    xx.add(x);
    i = i+2;
  }
  int antByte = 0;
  String a = ant.toRadixString(16);
  if(a.length == 1){
    antByte = int.parse("0x0$a");
  }else{
    antByte = int.parse("0x$a");
  }

  int block = settingEntity.blockCount??3;
  String b = block.toRadixString(16);
  int blockCountByte = 0;
  if(b.length == 1){
    blockCountByte = int.parse("0x0$b");
  }else{
    blockCountByte = int.parse("0x$b");
  }
  var array = [0xdd, 0x11, 0xef, 0x13, antByte, 0x23, 0x01];
  array.addAll(xx);
  array.addAll([0x00,blockCountByte,0x00,0x00]);
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

//混合询查
inventoryMixed(int ant){
  int antByte = int.parse("0x8${ant.toRadixString(16)}");
  var array = [0x11, 0x00, 0x19, 0x04, 0x00, 0x02,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00, antByte,0x14];
  var (byte1,byte2) = analyzeCRC(array);
  array.add(byte1);
  array.add(byte2);
  Uint8List bytes = Uint8List.fromList(array);
  print(bytes);
  return bytes;
}

(int, int) analyzeCRC(data) {
  try {
    int i, j;
    int current_crc_value = 0xFFFF;
    for (i = 0; i < data.length; i++) {
      current_crc_value = current_crc_value ^ (data[i] & 0xFF);
      for (j = 0; j < 8; j++) {
        if ((current_crc_value & 0x01) != 0) {
          current_crc_value = (current_crc_value >> 1) ^ 0x8408;
        } else {
          current_crc_value = (current_crc_value >> 1);
        }
      }
    }
    Uint8List dataResult = Uint8List.fromList([0,0]);
    dataResult[0]= (current_crc_value &0xFF);
    dataResult[1]= ((current_crc_value >> 8) & 0xFF);
    int byte1 = int.parse("0x${dataResult[0].toRadixString(16).padLeft(2, '0').toUpperCase()}");
    int byte2 = int.parse("0x${dataResult[1].toRadixString(16).padLeft(2, '0').toUpperCase()}");
    return (byte1,byte2);
  } catch (e ) {
    print(e);
    return (0x00,0x00);
  }
}


 calculateBCC(Uint8List data) {
  int bcc = 0;
  for (int b in data) {
    bcc ^= b;
  }
  return "0x${bcc.toRadixString(16).toUpperCase()}";
}

